package com.hzqy.web.ac.epg;


import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzqy.commons.utils.TokenkeyUtil;
import com.hzqy.service.epg.HappyServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.CmContentVo;
import com.hzqy.web.vo.activity.ActivityVoteUserVo;

@Controller
@RequestMapping("/")
public class HappyAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private HappyServiceBean happyService;


	/**
	 * 查询参赛人员名单
	 * @param ccv
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value="/super/kids_list")
	@ResponseBody
	public Object selectHappyList(CmContentVo ccv,String f_avu_activityid,int page,int pageSize) {
		Map<String,Object> map = new HashMap<String,Object>();
		String address=ccv.getF_cc_director();
		if ("-1".equals(address)) {
			ccv.setF_cc_director("");
		}else if ("0".equals(address)) {
			ccv.setF_cc_director("杭州市");
		}else if ("1".equals(address)) {
			ccv.setF_cc_director("宁波市");
		}else if ("2".equals(address)) {
			ccv.setF_cc_director("温州市");
		}else if ("3".equals(address)) {
			ccv.setF_cc_director("绍兴市");
		}else if ("4".equals(address)) {
			ccv.setF_cc_director("湖州市");
		}else if ("5".equals(address)) {
			ccv.setF_cc_director("嘉兴市");
		}else if ("6".equals(address)) {
			ccv.setF_cc_director("金华市");
		}else if ("7".equals(address)) {
			ccv.setF_cc_director("衢州市");
		}else if ("8".equals(address)) {
			ccv.setF_cc_director("舟山市");
		}else if ("9".equals(address)) {
			ccv.setF_cc_director("台州市");
		}else if ("10".equals(address)) {
			ccv.setF_cc_director("丽水市");
		}
		map = happyService.selectHappyList(ccv,f_avu_activityid,page,pageSize);
		return map;
	}
	/**
	 * 插入投票记录
	 * @param acvote
	 * @return
	 */
	@RequestMapping(value="/super/insertVoteUser")
	@ResponseBody
	public Object insertVoteUser(ActivityVoteUserVo acvote,String f_cc_code,String serverRandom,HttpSession session) {
		String userid= String.valueOf( session.getAttribute(SessionKey.SESSION_SERVERID));
		if (!userid.equals(acvote.getF_avu_serverid())) {
			LOG.debug("投票异常用户：epg_info session账户："+userid+"接口获取账户："+acvote.getF_avu_serverid()+"参赛编号："+f_cc_code);
			return  -2;
		}
		String tokenkey = HappyAction.token(userid);
		String attribute = String.valueOf(session.getAttribute("EPGverify"+userid));
		if (attribute=="null" || !attribute.equals(tokenkey)) {
			LOG.debug("attribute:" + attribute);
			LOG.debug("恶意攻击接口,session未获取到EPGverify:" + attribute);
			return -3;
		}
		String random=String.valueOf(session.getAttribute(userid+"random"));
		if (!random.equals(serverRandom)) {
			return  -4;
		}
		acvote.setF_avu_type(3);
		int toatl = happyService.selectSumVotenum(acvote);
		acvote.setF_avu_type(2);
		int used = happyService.selectSumVotenum(acvote);
		if (acvote.getF_avu_votenum() > (toatl - used)) {
			return  -1;
		}else {
			int f_cc_id=happyService.selectContentId(f_cc_code);
			if (f_cc_id==0) {
				return 0;
			}
			acvote.setF_avu_contentid(f_cc_id);
			acvote.setF_avu_activityid("44");
			acvote.setF_avu_source(0);
			acvote.setF_avu_memo("用户投票");
			acvote.setF_avu_obligate1(0);
			return happyService.insertVoteUser(acvote);
		}
	}

	/**
	 * 查询票数
	 * @param avu
	 * @return
	 */
	@RequestMapping(value="/super/selectServiceVotenum")
	@ResponseBody
	public Object selectServiceVotenum(ActivityVoteUserVo acvote,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		acvote.setF_avu_type(3);
		int toatl = happyService.selectSumVotenum(acvote);
		acvote.setF_avu_type(2);
		int used = happyService.selectSumVotenum(acvote);
		map.put("toatl", toatl);
		map.put("used", used);
		if ((toatl-used) < 0) {
			map.put("surplus", 0);
		}else {
			map.put("surplus", toatl-used);
		}
		return map;
	}

	public static String token(String userID) {
		String tokenkey = TokenkeyUtil.getTokenkey("zjgditv", userID, "super$^@%!*", "44activity");
		return tokenkey;
	}
	/**
	 * 生成四位随机数
	 * @param avu
	 * @return
	 */
	@RequestMapping(value="/super/getRandom")
	@ResponseBody
	public Object randomFour(HttpSession session){
		Map<String,Object> map=new HashMap<String, Object>();
		SecureRandom random = new SecureRandom();
		int i = (int) ((random.nextDouble() * 9 + 1) * 1000);
		map.put("rand",i);
		String userid= String.valueOf( session.getAttribute(SessionKey.SESSION_SERVERID));
		session.setAttribute(userid+"random", i);
		return map;
	}
}
